package gui.initiatives;

import gui.DateTimePanel;
import gui.EarthPanel;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import simulation.initiatives.AbstractEngine;

import common.SimulationSettings;

public abstract class AbstractClockControl extends JPanel implements ActionListener {
  private static final long serialVersionUID = -5822023900023271362L;

  protected DateTimePanel pnlDateTime;
  protected JButton btnRun;
  protected SimulationSettings settings;
  protected AbstractEngine engine;
  private EarthPanel pnlEarth;
  
  class Control {
    static final String start = "Start";
    static final String pause = "Pause";
    static final String reset = "Reset";
  }
  
  public AbstractClockControl(EarthPanel pnlEarth, SimulationSettings settings) {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    this.settings = settings;
    this.pnlEarth = pnlEarth;
    
    pnlDateTime = new DateTimePanel();
    add(pnlDateTime);
    addControls();
  }
    
  private void addControls() {
    btnRun = new JButton(Control.start);
    btnRun.addActionListener(this);
    btnRun.setActionCommand(Control.start);
    
    JButton btnReset = new JButton(Control.reset);
    btnReset.addActionListener(this);
    btnReset.setActionCommand(Control.reset);
    
    JPanel pnlButtons = new JPanel();
    pnlButtons.setLayout(new BoxLayout(pnlButtons, BoxLayout.LINE_AXIS));
    pnlButtons.add(btnRun);
    pnlButtons.add(Box.createRigidArea(new Dimension(10,10)));
    pnlButtons.add(btnReset);
    
    add(pnlButtons);
  }
  
  public void actionPerformed(ActionEvent evt) {
    if(evt.getActionCommand().equals(Control.start)) {
      startClock();
      btnRun.setText(Control.pause);
      btnRun.setActionCommand(Control.pause);
      pnlEarth.disableTooltips();
    }
    else if(evt.getActionCommand().equals(Control.pause)) {
      pauseClock();
      btnRun.setText(Control.start);
      btnRun.setActionCommand(Control.start);
      pnlEarth.enableTooltips();
    }
    else if(evt.getActionCommand().equals(Control.reset)) {
      resetClock();
      btnRun.setText("Start");
      btnRun.setActionCommand(Control.start);
      pnlEarth.enableTooltips();
    }    
  }
  
  public AbstractEngine getEngine() {
    return engine;
  }
  
  public void adjustBufferRate(int newRate) {
    //should be implemented by the buffer control class
  }
  
  public abstract void adjustDisplayRate(int newRate);
  public abstract void startClock();
  public abstract void pauseClock();
  public abstract void resetClock();
}
